package com.c.ctgproject.ui.service

import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.service.ServiceLogisticsAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.service.ServiceLogisticsEntity
import com.c.ctgproject.utils.*
import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.activity_service_logistics.*

/**
 *作者:LiBW
 *创建日期: 2021/7/5
 *描述: 物流管理 页面(列表)
 */
class ServiceLogisticsActivity : BaseActivity() {
    private val mViewModel by lazy { ViewModelProvider(this)[ServiceLogisticsViewModel::class.java] }

    //订单来源
    private var search = ""
    private var statue = ""
    private var page = 1

    //主列表
    private val mList = ArrayList<ServiceLogisticsEntity.DataBean.ListBean>()
    private val mAdapter by lazy { ServiceLogisticsAdapter(this, mList) }

    override fun getLayout() = R.layout.activity_service_logistics

    override fun event() {
        mLogisticsRV.adapter = mAdapter
        mLogisticsRV.layoutManager = LinearLayoutManager(this)

        mViewModel.error.observe(this, {
            loading.dismiss()
            mRefresh.over()
            MyLog.d("错误:$it")
            showToast(it)
        })

        mViewModel.result.observe(this, {
            loading.dismiss()
            mRefresh.over()
            MyLog.d("物流列表:$it")
            handlerHttpResult(it) {
                val data = getGson().fromJson(it, ServiceLogisticsEntity::class.java).data
                data?.list?.let {list->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            }
        })

    }

    override fun onResume() {
        super.onResume()
        getData(true)
    }

    fun getData(newData: Boolean = false) {
        loading.show()
        if (newData) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
            page = 1
        }
        mViewModel.getList(search, statue, page)
    }

    override fun click() {
        //搜索
        mSearchTV.setOnClickListener {
            val str = mCarNumberET.text.handlerNull()
            if (str.isNotEmpty()) {
                search = str
                getData(true)
            }
        }

        //清空输入框
        mCleanIV.setOnClickListener {
            mCarNumberET.textSet("")
        }
        mCarNumberET.handlerText {
            if (it.isEmpty()) {
                search = ""
                getData(true)
            }
        }

        mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mRefresh.setOnRefreshListener {
            getData(true)
        }
        mTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                statue = when (tab?.text.handlerNull()) {
                    "全部" ->  ""
                    "待处理" ->  "1"
                    "待托运" ->  "2"
                    "托运中" ->  "3"
                    "已完成" ->  "4"
                    "已退回" ->  "5"
                    else -> ""
                }
                getData(true)
                if (tab?.text.handlerNull()==""){

                }

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }
        })
    }


}